package com.ojoin.trade.common_core.web.dao.impl;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ojoin.trade.common_core.web.dao.TbMerchantuserFinaccountDao;
import com.ojoin.trade.common_core.web.domain.TbMerchantuserFinaccount;
import com.ojoin.trade.common_core.web.domain.TbMerchantuserFinaccountVo;

import org.springframework.stereotype.Repository;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-10-22
 * Time: 上午11:29
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TbMerchantuserFinaccountDaoImpl extends BaseDaoImpl<TbMerchantuserFinaccount> implements TbMerchantuserFinaccountDao {

//    @Override
//    public void insertTbMerchantuserFinaccount(int idOfTbMerchantUser, int businessType, String financeAccount) throws BusinessException {
//     TbMerchantuserFinaccount finaccountvo=new TbMerchantuserFinaccount();
//     finaccountvo.setBusinessType(businessType);
//     finaccountvo.setFinanceAccount(financeAccount);
//     finaccountvo.setIdOfTbMerchantUser(idOfTbMerchantUser);
//     String mapMethodName ="insert";
//        try{
//            this.sqlSessionTemplate.insert(getSqlName(mapMethodName),finaccountvo);
//        }catch (Exception e) {
//            throw new DaoException(String.format("插入商户金融结算户信息表 tb_merchantuser_finaccount出错！语句：%s",
//                    getSqlName(mapMethodName)), e);
//        }
//    }

    @Override
    public void insertTbMerchantuserFinaccount(TbMerchantuserFinaccount tbMerchantuserFinaccount) throws BusinessException {
        try{
            this.insert(tbMerchantuserFinaccount);
        }catch (Exception e) {
            throw new DaoException("插入商户金融结算户信息表 tb_merchantuser_finaccount出错！语句：%s", e);
        }
    }

    @Override
    public List<TbMerchantuserFinaccount> queryFinaccountByJoinid(Integer idOfTbMerchantUser,Integer businessType) throws BusinessException {
        HashMap<String,Object> paramsmap=new HashMap<String, Object>();
        paramsmap.put("id_of_tb_merchant_user",idOfTbMerchantUser);
        paramsmap.put("business_type",businessType);
        String mapMethodName="selectByJoinid";
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),
                    paramsmap);
            if (list != null && list.size() >0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("查询用户金融户->出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }

        return null;


    }

    @Override
    public List<TbMerchantuserFinaccountVo> queryFinaccountByType(int businessType,String accountType) throws BusinessException {

        String mapMethodName="selectByType";
        Map<String,Object> param=new HashMap<>();
        param.put("businessType",businessType);
        param.put("accountType",accountType);
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),
                    param);
            if (list != null && list.size() >0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("根据业务类型查询用户金融户->businesstype" + businessType + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }

        return null;
    }

    @Override
    public TbMerchantuserFinaccount queryFinaccount(Integer idOfTbMerchantUser, Integer businessType, String accountType) throws BusinessException {
        String mapMethodName="selectFinance";
        Map<String,Object> param=new HashMap<>();
        param.put("business_type",businessType);
        param.put("accountType",accountType);
        param.put("id_of_tb_merchant_user",idOfTbMerchantUser);
        try {
           List<TbMerchantuserFinaccount> list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),
                   param);
            if(!CommonUtils.isEmpty(list))
            return list.get(0);
        } catch (Exception e) {
            throw new DaoException(String.format("根据业务类型查询用户金融户->businesstype" + businessType + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
        return null;

    }

    @Override
    public List<TbMerchantuserFinaccount> queryFinaccountByJoinidAndAcctype(Integer idOfTbMerchantUser, String acccountType) throws BusinessException {
        String mapMethodName="selectFinanceByJoinid";
        Map<String,Object> param=new HashMap<>();
        param.put("accountType",acccountType);
        param.put("id_of_tb_merchant_user",idOfTbMerchantUser);
        try {
         return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),
                    param);
        } catch (Exception e) {
            throw new DaoException(String.format("根据账户类型查询用户金融户->acccountType" + acccountType + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }
}
